////Copyright 2011 - Jonathan Allen

//using System;
//using System.Linq;
//using System.ComponentModel;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Input;
//using System.Windows.Interactivity;

//namespace Granite.Xaml.Behaviors
//{
//    /// <summary>
//    /// This behavior causes the text to be automatically selected when the PasswordBox gets the focus.
//    /// </summary>
//	public sealed partial class SelectAllPasswordBoxBehavior : Behavior<PasswordBox>
//    {
//		/// <summary>
//		/// Determines if this behavior is in effect and if so when Select All is triggered.
//		/// </summary>
//        public static readonly DependencyProperty ModeProperty = DependencyProperty.RegisterAttached("Mode", typeof(SelectAllMode), typeof(SelectAllPasswordBoxBehavior), new PropertyMetadata(SelectAllMode.Never, OnModeChanged));

//        bool m_AlreadyFocused;

//        bool m_MouseIsDown;

//		/// <summary>
//		/// Determines if this behavior is in effect and if so when Select All is triggered.
//		/// </summary>
//		public SelectAllMode Mode { get; set; }

//		/// <summary>
//		/// Determines if this behavior is in effect and if so when Select All is triggered.
//		/// </summary>
//		public static SelectAllMode GetMode(DependencyObject control)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            return (SelectAllMode)(control.GetValue(ModeProperty));
//        }

//		/// <summary>
//		/// Determines if this behavior is in effect and if so when Select All is triggered.
//		/// </summary>
//		public static void SetMode(DependencyObject control, SelectAllMode value)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            control.SetValue(ModeProperty, value);
//        }

//        void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
//        {
//            if (!m_MouseIsDown)
//                m_AlreadyFocused = true;

//            switch (Mode)
//            {
//                case SelectAllMode.Never:
//                    break;

//                case SelectAllMode.TabOnly:
//                    if (!m_MouseIsDown)
//                        AssociatedObject.SelectAll();
//                    break;

//                case SelectAllMode.Always:
//                    AssociatedObject.SelectAll();
//                    break;
//            }
//        }

//        void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
//        {
//            m_AlreadyFocused = false;
//            m_MouseIsDown = false;
//        }

//        void AssociatedObject_MouseDown(object sender, MouseButtonEventArgs e)
//        {
//            m_MouseIsDown = true;
//        }

//        void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs e)
//        {
//            if (!m_AlreadyFocused)
//            {
//                m_AlreadyFocused = true;
//                if (Mode == SelectAllMode.Always)
//                    AssociatedObject.SelectAll();
//            }
//        }

//        static void OnModeChanged(DependencyObject control, DependencyPropertyChangedEventArgs e)
//        {
//            var uie = control as UIElement;
//            if (uie == null) return;

//            var behaviors = Interaction.GetBehaviors(uie);
//            var existingBehavior = behaviors.OfType<SelectAllPasswordBoxBehavior>().FirstOrDefault();
//            var newValue = (SelectAllMode)e.NewValue;

//            if (newValue == SelectAllMode.Never)
//            {
//                if (existingBehavior != null)
//                    behaviors.Remove(existingBehavior);
//            }
//            else
//            {
//                if (existingBehavior != null)
//                    existingBehavior.Mode = newValue;
//                else
//                    behaviors.Add(new SelectAllPasswordBoxBehavior() { Mode = newValue });
//            }
//        }

//    }

//#if WPF
//    public partial class SelectAllPasswordBoxBehavior : Behavior<PasswordBox>
//    {
//		/// <summary>
//		/// 
//		///             Called after the behavior is attached to an AssociatedObject.
//		///             
//		/// </summary>
//		/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
//        protected override void OnAttached()
//        {
//            if (!DesignerProperties.GetIsInDesignMode(AssociatedObject))
//            {
//                AssociatedObject.GotFocus += AssociatedObject_GotFocus;
//                AssociatedObject.LostFocus += AssociatedObject_LostFocus;

//                AssociatedObject.PreviewMouseDown += AssociatedObject_MouseDown;
//                AssociatedObject.PreviewMouseUp += AssociatedObject_MouseUp;

//            }
//        }

//		/// <summary>
//		/// 
//		///             Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
//		///             
//		/// </summary>
//		/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
//        protected override void OnDetaching()
//        {
//            if (!DesignerProperties.GetIsInDesignMode(AssociatedObject))
//            {
//                AssociatedObject.GotFocus -= AssociatedObject_GotFocus;
//                AssociatedObject.LostFocus -= AssociatedObject_LostFocus;

//                AssociatedObject.PreviewMouseDown -= AssociatedObject_MouseDown;
//                AssociatedObject.PreviewMouseUp -= AssociatedObject_MouseUp;
//            }

//        }

//    }
//#endif

//#if SILVERLIGHT
//    public partial class SelectAllPasswordBoxBehavior : Behavior<PasswordBox>
//    {
//		/// <summary>
//		/// 
//		///             Called after the behavior is attached to an AssociatedObject.
//		///             
//		/// </summary>
//		/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
//        protected override void OnAttached()
//        {
//            if (!DesignerProperties.GetIsInDesignMode(AssociatedObject))
//            {
//                AssociatedObject.GotFocus += AssociatedObject_GotFocus;
//                AssociatedObject.LostFocus += AssociatedObject_LostFocus;

//                AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseUp;
//                AssociatedObject.MouseRightButtonUp += AssociatedObject_MouseUp;
//                AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseDown;
//                AssociatedObject.MouseRightButtonDown += AssociatedObject_MouseDown;

//            }
//        }

//		/// <summary>
//		/// 
//		///             Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
//		///             
//		/// </summary>
//		/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
//        protected override void OnDetaching()
//        {
//            if (!DesignerProperties.GetIsInDesignMode(AssociatedObject))
//            {
//                AssociatedObject.GotFocus -= AssociatedObject_GotFocus;
//                AssociatedObject.LostFocus -= AssociatedObject_LostFocus;

//                AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseUp;
//                AssociatedObject.MouseRightButtonUp -= AssociatedObject_MouseUp;
//                AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseDown;
//                AssociatedObject.MouseRightButtonDown -= AssociatedObject_MouseDown;

//            }

//        }
//    }
//#endif
//}

